﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ShootSearch.FileSystem;
using System.IO;
using ShootSearch.Util.Serializers;

namespace ShootSearch.Tools
{
    public partial class FormBrowserFS : Form
    {
        IFileSystem fs = null;

        public FormBrowserFS()
        {
            InitializeComponent();
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (Directory.Exists(comboBoxPath.Text))
            {
                fbd.SelectedPath = comboBoxPath.Text;
            }
            else fbd.SelectedPath = Application.StartupPath;

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                fs = new STFileSystem();
                fs.CreateOpen(fbd.SelectedPath);
                comboBoxPath.Text = fbd.SelectedPath;

                listViewFiles.Items.Clear();
                foreach (var item in fs.ListFileNames())
                {
                    listViewFiles.Items.Add(new ListViewItem(item));
                }
            }
        }

        private void listViewFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fs==null || listViewFiles.SelectedItems.Count == 0) return;

            IFileObject fo = fs.Read(listViewFiles.SelectedItems[0].Text);
            if (fo.ContentType == FileObjectText.ID)
            {
                FileObjectText fot = new FileObjectText((FileObject)fo);
                textBoxSource.Text = XmlFormatter.ToXml(fot);
            }
            else
            {
                textBoxSource.Text = fo.Encoding.GetString(fo.Content);
            }

        }
    }
}
